import { Component, OnInit } from '@angular/core';
import { YYCTypeListInfo } from 'src/model/yy/YYCTypeListInfo';
import { YY1API } from 'src/api/YY1API';
import { Router } from '@angular/router';

@Component({
  selector: 'app-yy1-tab',
  templateUrl: './yy1-tab.page.html',
  styleUrls: ['./yy1-tab.page.scss'],
})
export class Yy1TabPage implements OnInit {

  // 列表
  list: Array<YYCTypeListInfo>;

  constructor(
    private yy1API: YY1API,
    private router: Router
  ) { }

  ngOnInit() {
    // 初始化
    this.initPage();
  }

  // 查询
  async search(showLoader: boolean = true): Promise<Array<YYCTypeListInfo>> {
    // 查询列表
    const lstInfo = (await this.yy1API.queryCType(showLoader)).lstInfo;

    // 条件过滤页入口
    let filterInfo = new YYCTypeListInfo();
    filterInfo.cType = '过滤';
    lstInfo.push(filterInfo);

    return lstInfo;
  }

  // 初始化
  async initPage() {
    // 查询
    this.list = await this.search();
  }

  // 刷新
  async doRefresh(event: any) {
    // 查询
    this.list = await this.search(false);

    // 刷新完成
    event.target.complete();
  }

  // 跳转列表页面
  async openList(item: YYCTypeListInfo) {
    if (item.cType === '过滤') {
      this.router.navigate(
        ['tabs/yy1Tab/yy1Filter'],
        { queryParams: { cType: item.cType } });
    } else {
      this.router.navigate(
        ['tabs/yy1Tab/yy1List'],
        { queryParams: { cType: item.cType } });
    }
  }
}
